<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: magic_move.php 6752 2010-03-25 08:47:54Z cnteacher $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

if(submitcheck('usesubmit')) {

	if(empty($_G['tid']) && empty($moveto)) {
		showmessage('magics_info_nonexistence');
	}

	$thread = getpostinfo($_G['tid'], 'tid', array('fid', 'tid', 'authorid', 'special'));
	checkmagicperm($magicperm['forum'], $thread['fid']);

	if($thread['authorid'] != $_G['uid']) {
		showmessage('magics_operation_nopermission');
	}

	if($thread['special']) {
		$query = DB::query("SELECT allowpostspecial FROM ".DB::table('forum_forum')." WHERE fid='$moveto'");
		if(!substr(sprintf('%04b', $_G['forum']['allowpostspecial']), -$thread['special'], 1)) {
			showmessage('admin_move_nopermission');
		}
	}

	$query = DB::query("SELECT postperm FROM ".DB::table('forum_forumfield')." WHERE fid='$moveto'");
	if($forum = DB::fetch($query)) {
		if(!$forum['postperm'] && !$_G['group']['allowpost']) {
			showmessage('group_nopermission', NULL, array('grouptitle' => $_G['group']['grouptitle']), array('login' => 1));
		} elseif($forum['postperm'] && !forumperm($forum['postperm'])) {
			showmessage('post_forum_newthread_nopermission');
		}
	}

	DB::query("UPDATE ".DB::table('forum_thread')." SET fid='$moveto', moderated='1' WHERE tid='$_G[tid]'");
	$posttable = getposttablebytid($_G['tid']);
	DB::query("UPDATE ".DB::table($posttable)." SET fid='$moveto' WHERE tid='$_G[tid]'");

	require_once libfile('function/post');
	updateforumcount($moveto);
	updateforumcount($thread['fid']);

	usemagic($magicid, $magic['num']);
	updatemagiclog($magicid, '2', '1', '0', $_G['tid']);
	updatemagicthreadlog($_G['tid'], $magicid, $magic['identifier']);
	showmessage('magics_operation_succeed', '', array('succeed' => 1));

}

function showmagic() {
	global $_G, $lang;
	require_once libfile('function/forumlist');
	magicshowtype($lang['option'], 'top');
	magicshowsetting($lang['target_tid'], 'tid', $_G['tid'], 'text');
	magicshowsetting($lang['MVK_target'], '', '', '<select name="moveto">'.forumselect().'</select>');
	magicshowtype('', 'bottom');
}

?>